package command

import (
	"errors"
	"strings"
	"time"

	"ssforward/common/dto"
	"ssforward/common/enums"
	"ssforward/common/utils"
	"ssforward/internal/forward/models"
)

type ForwardServerInsertReq struct {
	Name         string                  `json:"name"`
	Type         string                  `json:"type" vd:"len($)>0"`
	Host         string                  `json:"host" vd:"len($)>0"`
	Port         int                     `json:"port"`
	Url          string                  `json:"url"`
	Username     string                  `json:"username"`
	Password     string                  `json:"password"`
	DbName       string                  `json:"dbName"`
	DbDriver     string                  `json:"dbDriver"`
	DbTable      string                  `json:"dbTable"`
	ContentType  string                  `json:"contentType"`
	Header       *map[string]interface{} `json:"header"`
	dto.UserInfo `search:"-"`
}

func (d *ForwardServerInsertReq) Generate(model *models.SysForwardServer) error {
	model.Id = utils.SimpleUUID()
	model.Name = strings.TrimSpace(d.Name)
	model.Type = d.Type
	if len(model.Name) == 0 {
		model.Name = model.Type + "-" + model.Id
	}
	model.Host = d.Host
	model.Port = d.Port
	model.Url = d.Url
	model.Username = d.Username
	model.Password = d.Password
	model.DbName = d.DbName
	model.DbDriver = d.DbDriver
	model.DbTable = d.DbTable
	now := dto.LocalTime(time.Now())
	model.CreateTime = &now
	model.CreatorId = d.UserId
	model.AgencyId = d.AgencyId
	model.ContentType = d.ContentType
	if d.Header != nil {
		model.Header = utils.MapToBytes(*d.Header)
	}
	// 校验服务类型
	serverType := enums.GetServerType(model.Type)
	if len(serverType.Value) == 0 {
		return errors.New("参数异常,服务类型不支持")
	}
	if serverType.Value == enums.Http.Get().Value {
		if len(model.Url) == 0 {
			return errors.New("参数异常,HTTP服务接口URL不完整")
		}
	} else if serverType.Value == enums.Database.Get().Value {
		if len(model.DbName) == 0 || len(model.DbDriver) == 0 {
			return errors.New("参数异常,DB服务请指定数据库名称/驱动类型")
		}
	}
	return nil
}

type ForwardServerEditReq struct {
	Id           string                  `json:"id" vd:"len($)>0"`
	Name         string                  `json:"name"`
	Host         string                  `json:"host"`
	Port         int                     `json:"port"`
	Url          string                  `json:"url"`
	Username     string                  `json:"username"`
	Password     string                  `json:"password"`
	DbName       string                  `json:"dbName"`
	DbDriver     string                  `json:"dbDriver"`
	DbTable      string                  `json:"dbTable"`
	ContentType  string                  `json:"contentType"`
	Header       *map[string]interface{} `json:"header"`
	dto.UserInfo `search:"-"`
}

func (d *ForwardServerEditReq) Generate(model *models.SysForwardServer) {
	// 可编辑项
	if len(d.Name) > 0 {
		model.Name = d.Name
	}
	if len(d.Host) > 0 {
		model.Host = d.Host
	}
	if d.Port > 0 {
		model.Port = d.Port
	}
	if len(d.Url) > 0 {
		model.Url = d.Url
	}
	if len(d.Username) > 0 {
		model.Username = d.Username
	}
	if len(d.Password) > 0 {
		model.Password = d.Password
	}
	if len(d.DbName) > 0 {
		model.DbName = d.DbName
	}
	if len(d.DbDriver) > 0 {
		model.DbDriver = d.DbDriver
	}
	if len(d.DbTable) > 0 {
		model.DbTable = d.DbTable
	}
	if len(d.ContentType) > 0 {
		model.ContentType = d.ContentType
	}
	if d.Header != nil {
		model.Header = utils.MapToBytes(*d.Header)
	}
	now := dto.LocalTime(time.Now())
	model.UpdateTime = &now
}

type ServerDeleteReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}
